JavaScript: Learn Basics of Scripting Language and Use in Programming Easily(javascript advanced,javascript algorithm,javascript and jquery,javascript beginners guide,javascript interview) by James Jackson

JavaScript: Learn Basics of Scripting Language and Use in Programming Easily(javascript advanced,javascript algorithm,javascript and jquery,javascript beginners guide,javascript interview) by James Jackson

Author:James Jackson
Language: eng
Format: azw3, epub, pdf
Publisher: Kashvi Publishing
Published: 2016-12-03T08:00:00+00:00


TheFor loop is a primary example of a pre-condition loop. The loop is pretty simple and will keep on repeating itself until the specified condition has been fulfilled, or in other words, the specified condition has been deemed false.

The skeleton of the For loop is –

for( [initialExpression] ; [condition] ; [incrementExpression])

{

Statement

}

For example, say we want to print the String, Hello World! five times. We can either do this by repeating the command console.log(“Hello World!”); five times or we can create a for loop as follows:

for (vari = 0; i< 5; i++) {

console.log(“Hello World!”);

}

Whenever the conditions of a For loop is satisfied, the following processes are happening inside of it-

If the conditions are met, then the initializing expression (if any) will be executed. In the above example, the initial expression sets the variable i (which serves as a counter) to 0.

After which, the given condition will be evaluated. If the found condition is true, then the loop will keep on executing itself, until it has reached a state where the given condition appears to be false, in which case the loop will stop. If however, the condition expression has been completely removed, then the loop will keep on executing itself infinitely.The condition is that i should be less than 5.

Once the loop has executed itself, it will look inside for a statement which it needs to be run. If you want to have multiple statements inside your loop, then you can use a block statement ({…}) to separate those statements in particular groups.

Once the statement running is complete, the loop will look at the incrementExpression, where it will update the Initial Expression and keep on repeating the cycle over and over again. On the first loop, i = 0. On the second loop, i = 1. Thus, you can see that it will take five loops for the condition to return a Boolean value of false where i< 5 (0, 1, 2, 3, 4). On the sixth loop, i will equal 5, making the condition false. It is important to note that you can also use any comparison operator for the condition. In this way, for(vari = 0; i< 5; i++) and for (vari = 0; i<=4; i++) will both run the same number of loops.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.